<template>
  <div class="addCategory">
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="35%"
      :before-close="categoryClose">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="form.name" size="small"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="form.sort" size="small"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="categoryClose" size="small">取 消</el-button>
        <el-button type="primary" size="small" @click="doSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { categoryUpdate } from '@/services/resource'
export default {
  name: 'addCategory',
  data () {
    return {
      dialogVisible: false,
      form: {},
      title: '添加分类'
    }
  },
  methods: {
    // 关闭弹窗
    categoryClose () {
      this.dialogVisible = false
    },
    // 打开弹窗
    categoryOpen () {
      this.dialogVisible = true
    },
    // 保存
    async doSave () {
      const { data } = await categoryUpdate(this.form)
      if (data.code === '000000') {
        this.$message.success('数据保存成功')
      } else {
        this.$message.error('数据保存失败')
      }
      // 关闭弹窗
      this.categoryClose()
      // 刷新列表页面
      this.$emit('refreshCategory')
    },
    // 父组件调用子组件的方法,用于编辑页的数据展示
    getcategoryEdit (val) {
      this.form = val
      this.title = '编辑分类'
    }
  }
}
</script>

<style lang='scss' scoped>

</style>
